#include <stdio.h>
#include <stdlib.h>

typedef struct LNode* PtrToLNode;
struct LNode {
    int coef;
    int expon;
    PtrToLNode next;
};

int insert ( int c, int e, PtrToLNode rear ) {
    if ( e == rear->expon ) {
        rear->coef+=c;
        return 1;
    } else {
        PtrToLNode tmp = (PtrToLNode)malloc(sizeof(struct LNode));
        tmp->coef = c;
        tmp->expon = e;
        tmp->next = rear->next;
        rear->next = tmp;
        return 0;
    }
}

void freenode ( PtrToLNode node, int state ) {
    PtrToLNode tmp = node;
    int output = 0;
    node = tmp->next;
    free(tmp);
    if ( !node ) {
        if ( state == 1 ) printf("0 0\n");
        return;
    } else {
        tmp = node;
        if ( state == 1 && tmp->coef != 0 ) {
            printf("%d %d", tmp->coef, tmp->expon);
            output = 1;
        }
        node = tmp->next;
        free(tmp);
    }
    while ( node ) {
        tmp = node;
        if ( state == 1 && tmp->coef != 0 ) {
            printf(" %d %d", tmp->coef, tmp->expon);
            output = 1;
        }
        node = tmp->next;
        free(tmp);
    }
    if ( state == 1 && output == 1 ) printf("\n");
    if ( state == 1 && output == 0 ) printf("0 0\n");
}

int main () {
    PtrToLNode L1 = (PtrToLNode)malloc(sizeof(struct LNode)), L2 = (PtrToLNode)malloc(sizeof(struct LNode));
    PtrToLNode plus = (PtrToLNode)malloc(sizeof(struct LNode)), multiple = (PtrToLNode)malloc(sizeof(struct LNode));
    L1->next = NULL; L1->expon = -2000; L1->next = NULL; L2->next = NULL; L2->expon = -2000;
    plus->next = NULL; multiple->next = NULL; multiple->expon = 0;
    PtrToLNode p_to_L1 = L1, p_to_L2 = L2, p_to_plus = plus, p_to_multiple = multiple;
    int N, coef, expon;
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d %d", &coef, &expon);
        insert(coef, expon, p_to_L1);
        p_to_L1 = p_to_L1->next;
    }
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d %d", &coef, &expon);
        insert(coef, expon, p_to_L2);
        p_to_L2 = p_to_L2->next;
    }
    /* plus */
    p_to_L1 = L1->next; p_to_L2 = L2->next;
    while ( p_to_L1 != NULL && p_to_L2 != NULL ) {
        if ( p_to_L1->expon >= p_to_L2->expon ) {
            if ( !insert(p_to_L1->coef, p_to_L1->expon, p_to_plus) ) p_to_plus = p_to_plus->next;
            p_to_L1 = p_to_L1->next;
        } else if ( p_to_L1->expon < p_to_L2->expon ) {
            if ( !insert(p_to_L2->coef, p_to_L2->expon, p_to_plus) ) p_to_plus = p_to_plus->next;
            p_to_L2 = p_to_L2->next;
        }
        
    }
    if ( p_to_L1 ) {
        while ( p_to_L1 ) {
            if ( !insert(p_to_L1->coef, p_to_L1->expon, p_to_plus) ) p_to_plus = p_to_plus->next;
            p_to_L1 = p_to_L1->next;
        }
    } else if ( p_to_L2 ) {
        while ( p_to_L2 ) {
            if ( !insert(p_to_L2->coef, p_to_L2->expon, p_to_plus) ) p_to_plus = p_to_plus->next;
            p_to_L2 = p_to_L2->next;
        }
    }

    /* multiple */
    p_to_L1 = L1->next; p_to_L2 = L2->next;
    while ( p_to_L1 ) {
        while ( p_to_L2 ) {
            p_to_multiple = multiple;
            expon = p_to_L1->expon+p_to_L2->expon;
            while ( p_to_multiple->next && expon <= p_to_multiple->next->expon ) p_to_multiple = p_to_multiple->next;
            insert(p_to_L1->coef*p_to_L2->coef, expon, p_to_multiple);
            p_to_L2 = p_to_L2->next;
        }
        p_to_L1 = p_to_L1->next;
        p_to_L2 = L2->next;
    }
    
    freenode(L1,0); freenode(L2,0); freenode(multiple,1); freenode(plus,1);
    return 0;
}